import { addAiChatgptKeysApi } from "@/api/aiTask";
import { useVModel } from "@vueuse/core";
import { Modal, Textarea, message } from "ant-design-vue";

type AddChatgptKeyComponentProps = {
    visible: boolean;
};
// 添加组合规则时，动态添加删除内容项
export const AddChatgptKeyComponent = defineComponent({
    props: ["visible"],
    emits: ["update:visible"],
    setup(props: AddChatgptKeyComponentProps, { emit }) {
        const visible = useVModel(props, "visible", emit);
        const value = ref("");
        const onOk = () => {
            const keys = value.value.split("\n").filter(Boolean);
            addAiChatgptKeysApi(keys)
                .then((res) => {
                    if (res.data.code == 200) {
                        message.success("添加成功!");
                        return;
                    }
                })
                .catch(() => {
                    message.error("操作失败!");
                })
                .finally(() => {
                    visible.value = false;
                    value.value = "";
                });
        };
        return () => (
            <Modal v-model:open={visible.value} title="请输入ChatgptKey" onOk={onOk} style="width:450px">
                <Textarea
                    v-model:value={value.value}
                    rows={3}
                    style="width:400px"
                    placeholder="输入ChatgptKey 一行一个!"
                ></Textarea>
            </Modal>
        );
    }
});
